# import pytest
from selenium import webdriver
import unittest, time
from contrast import ImageCompare
# from ddt import ddt,data
from public import *
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import shutil
import datetime
import logging
from logger import *
import os
# from PIL import ImageGrab
import requests
import xlrd, xlwt
from xlutils.copy import copy
from test_send_email import *

# class TestAutomation:
import sys

sys.setrecursionlimit(10000)

number = 18328578991
pwd = 'abc123'
# 拷贝驱动文件到目录下
dir1 = 'C:\\CTDebug\HandSwitch\\HandSwitchOn.txt'
dir2 = 'C:\\CTDebug'


def set_sum(i):
    sum = i
    return sum

# 等待找到元素
def kw(driver, times, time, func):
    return WebDriverWait(driver, times, time, ignored_exceptions='元素异常').until(func)


file_name = None


def create_excel(d):
    global file_name
    ws = xlwt.Workbook()
    sheet = ws.add_sheet("CBCT曝光上传")
    sheet.write(0, 0, '虚拟驱动自动化测试')
    sheet.write(0, 1, "设置运行次数")
    sheet.write(1, 1, "{}".format(d))
    sheet.write(2, 1, "{}".format(d))
    sheet.write(3, 1, "{}".format(d))
    sheet.write(4, 1, "{}".format(d))
    sheet.write(0, 2, "实际运行次数")
    sheet.write(0, 3, "上传成功次数")
    sheet.write(0, 4, "上传失败次数")
    sheet.write(0, 5, "卡住次数")
    sheet.write(0, 6, '归档成功次数')
    sheet.write(0, 7, '归档失败次数')
    sheet.write(1, 0, "CT上传")
    sheet.write(2, 0, "PX上传")
    sheet.write(3, 0, "DX上传")
    sheet.write(4, 0, "MS上传")
    file_name = ".\\reports\\CBCT_上传测试{}.xls".format(time.strftime('%Y-%m-%d-%H'))
    ws.save(file_name)


def write_excel(a, b, value):
    data = xlrd.open_workbook(file_name)
    workbook = copy(data)
    sheet_write = workbook.get_sheet(0)
    value = str(value)
    sheet_write.write(a, b, value)
    workbook.save(file_name)


def login_():
    driver = webdriver.Chrome()
    driver.get('http://127.0.0.1:8189/#/')
    driver.implicitly_wait(120)
    driver.maximize_window()
    driver.find_element_by_xpath('//*[@id="app"]/div/div/div[1]/div[1]/form/div[1]/div/div/div/input').send_keys(
        number)  # 输入手机号
    driver.find_element_by_xpath('//*[@id="app"]/div/div/div[1]/div[1]/form/div[2]/div/div/div/input').send_keys(
        pwd)  # 输入密码
    driver.find_element_by_xpath('//div[@class="zh_postForm"]').click()  # 点击登录
    return driver


# global i


ct_a = 0  # 上传成功
ct_b = 0  # 上传失败
ct_c = 0  # 准备进度卡住
ct_d = 0  # 归档成功
ct_e = 0  # 归档失败


def create_contexts(path):
    isExists = os.path.exists(path)
    if not isExists:
        print(path)
        os.mkdir(path)
        return path

    else:
        # print('已存在！')
        return path


def create_test_contexts(path):
    isExists = os.path.exists(path)
    if not isExists:
        print(path)
        os.makedirs(path)
        return path

    else:
        # print('已存在！')
        return path


def test_01_CT_shooting(i, c, path, path2, path3):
    logging.info('1.CT开始时间{}'.format(str(datetime.datetime.now().strftime("%Y%m%d%H%M%S"))))
    logging.info('test_01_CT_shooting第{}次'.format(i))
    write_excel(1, 2, i)
    if c:
        driver = c
        # logging.info('test_01_CT_shooting_第{}次'.format(i))
        time.sleep(8)

        driver.find_element_by_xpath('//div[text()="登记"]').click()  # 进入登记页
        time.sleep(1)
        patient_name = random_name()
        driver.find_element_by_xpath('//input[@placeholder="患者姓名" and @class="yg-input__inner" ]').send_keys(
            "{}".format(patient_name))  # 输入患者姓名参数化
        time.sleep(1)
        driver.find_element_by_xpath('//div[@id="cbctCT"]').click()  # 选择拍摄模式
        driver.find_element_by_xpath('//div[@class="check-in-wrap"]/following-sibling::div[1]//div[2]').click()  # 点击下一步
        time.sleep(1)
        driver.find_element_by_xpath('//button[@id="cbctOkct"]').click()  # 点击确确定

        try:
            progress_locator = (By.XPATH, '//div[contains(text()," 准备完成")]')  # 等待准备完成元素可见
            WebDriverWait(driver, 90, 1).until(EC.visibility_of_element_located(progress_locator))
            time.sleep(1)
            driver.find_element_by_xpath('//div[@id="cbctFinishct"]').click()  # CT点击下一步
            shutil.copy(dir1, dir2)  # 复制手闸文件到CTDebug目录
            time.sleep(120)  # 等待采集曝光完成

            # 智能等待-等待元素可见与不可见--------------------------------------
            # time.sleep(2)
            # WebDriverWait(driver, 1200, 3).until(EC.visibility_of_element_located((By.XPATH,'//span[@class="nsel close-btn"]')))
            # WebDriverWait(driver, 1200, 3).until_not(EC.visibility_of_element_located((By.XPATH, '//div[@class="circle_proc"]')))
            # time.sleep(2)

            dic_file = create_contexts(path)  #
            filepath = '{}\\{}.png'.format(dic_file, str(datetime.datetime.now().strftime("%Y%m%d%H%M%S")))
            driver.save_screenshot(filepath)
            time.sleep(3)


            driver.find_element_by_xpath('//span[@class="nsel close-btn"]').click()  # CT叉掉元素
            time.sleep(5)
            driver.find_element_by_xpath('//div[text()="已检查"]').click()  # 已检查元素
            try:
                loc_upload = (By.XPATH, '//div[contains(text(),"已上传")]')
                if WebDriverWait(driver, 150, 1).until(EC.visibility_of_all_elements_located(loc_upload)):
                    logging.info('CT上传成功')
                    global ct_a
                    ct_a += 1
                    write_excel(1, 3, ct_a)
            except:
                logging.info('CT上传失败')
                global ct_b

                ct_b += 1
                write_excel(1, 4, ct_b)
            # 归档牙医管家
            driver.find_element_by_xpath('//*[@id="app"]/div/div/div[3]/div[2]/div[2]/div[4]/div/div').click()
            button = driver.find_element_by_xpath(
                '//*[@id="app"]/div/div/div[3]/div[2]/div[1]/div[3]/div/div[3]/button[1]')
            time.sleep(1)
            button.click()
            try:
                uploaded = (By.XPATH, '/html/body/div[2]/div/p')
                if WebDriverWait(driver, 150, 1).until(EC.visibility_of_all_elements_located(uploaded)):
                    logging.info('CT归档牙医管家成功')
                    global ct_d
                    ct_d += 1
                    write_excel(1, 6, ct_d)
            except:
                logging.info('CT归档牙医管家失败')
                global ct_e
                ct_e += 1
                write_excel(1, 7, ct_e)
                upload_error = create_test_contexts(path3)
                upload_filepath = '{}\\{}.png'.format(upload_error,
                                                 str(datetime.datetime.now().strftime("%Y%m%d%H%M%S")))  # 存放在
                driver.save_screenshot(upload_filepath)

            logging.info('CT结束时间{}'.format(str(datetime.datetime.now().strftime("%Y%m%d%H%M%S"))))
        except:
            dics_files = create_test_contexts(path2)
            error_filepath = '{}\\{}.png'.format(dics_files,
                                                 str(datetime.datetime.now().strftime("%Y%m%d%H%M%S")))  # 存放在
            driver.save_screenshot(error_filepath)
            global ct_c
            ct_c += 1
            write_excel(1, 5, ct_c)
            logging.error("CT{}进度卡住！！！".format(str(datetime.datetime.now().strftime("%Y%m%d%H%M%S"))))
            driver.quit()


px_a = 0
px_b = 0
px_c = 0  # 准备进度卡住
px_d = 0  # 归档成功
px_e = 0  # 归档失败


def test_02_Panoramic_shooting(i, a, path, path2, path3):
    driver2 = a
    logging.info('2.PX开始时间{}'.format(str(datetime.datetime.now().strftime("%Y%m%d%H%M%S"))))
    logging.info('PX第{}次'.format(i))
    write_excel(2, 2, i)
    time.sleep(3)
    driver2.find_element_by_xpath('//div[text()="登记"]').click()
    time.sleep(1)
    patient_name2 = random_name()
    driver2.find_element_by_xpath('//input[@placeholder="患者姓名" and @class="yg-input__inner" ]').send_keys(
        "{}".format(patient_name2))  # 输入患者姓名参数化
    driver2.find_element_by_xpath('//div[@id="cbctPX"]').click()  # 全景模式
    driver2.find_element_by_xpath(
        '//div[@class="check-in-wrap"]/following-sibling::div[1]//div[2]').click()  # 选择模式后的下一步
    time.sleep(3)
    driver2.find_element_by_xpath('//button[@id="cbctOkpano"]').click()  # 点击确定
    '''去曝光'''
    try:
        # time.sleep(60)
        progress_locator = (By.XPATH, '//div[contains(text()," 准备完成")]')  # 等待准备完成元素可见
        WebDriverWait(driver2, 150, 1).until(EC.visibility_of_element_located(progress_locator))
        time.sleep(1)
        driver2.find_element_by_xpath('//div[@id="cbctFinishceph"]').click()  # 全景下一步
        time.sleep(3)
        shutil.copy(dir1, dir2)  # 复制手闸文件到CTDebug目录
        # time.sleep(240)  # 全景曝光与采集时间
        ele2 = '//*[@id="leftwidth"]/div/div[5]/div/span/div[4]'
        kw(driver2, 50000, 1, lambda x: x.find_element_by_xpath(ele2))
        # 智能等待 加载进度条页面找不到
        # time.sleep(2)
        # loc = (By.XPATH, '//div[@class="circle_proc"]')
        # WebDriverWait(driver2, 1800, 3).until_not(EC.visibility_of_element_located(loc))
        # time.sleep(2)

        dic_file = create_contexts(path)  #
        filepath = '{}\\{}.png'.format(dic_file, str(datetime.datetime.now().strftime("%Y%m%d%H%M%S")))
        driver2.save_screenshot(filepath)

        driver2.find_element_by_xpath('//div[@id="cbctbcpano"]').click()  # 全景对勾元素确定
        time.sleep(6)
        driver2.find_element_by_xpath('//div[text()="已检查"]').click()  # 已检查元素
        try:
            loc_upload = (By.XPATH, '//div[contains(text(),"已上传")]')
            if WebDriverWait(driver2, 135, 1).until(EC.visibility_of_all_elements_located(loc_upload)):
                logging.info('PX上传成功')
                global px_a
                px_a += 1
                write_excel(2, 3, px_a)
        except:
            global px_b
            px_b += 1
            write_excel(2, 4, px_b)
            logging.info('PX上传失败')

        # 归档牙医管家
        driver2.find_element_by_xpath('//*[@id="app"]/div/div/div[3]/div[2]/div[2]/div[4]/div/div').click()
        button = driver2.find_element_by_xpath(
            '//*[@id="app"]/div/div/div[3]/div[2]/div[1]/div[3]/div/div[3]/button[1]')
        time.sleep(1)
        button.click()
        try:
            uploaded = (By.XPATH, '/html/body/div[2]/div/p')
            if WebDriverWait(driver2, 150, 1).until(EC.visibility_of_all_elements_located(uploaded)):
                logging.info('PX归档牙医管家成功')
                global px_d
                px_d += 1
                write_excel(2, 6, px_d)
        except:
            logging.info('PX归档牙医管家失败')
            global px_e
            px_e += 1
            write_excel(2, 7, px_e)
            upload_error = create_test_contexts(path3)
            upload_filepath = '{}\\{}.png'.format(upload_error,
                                                  str(datetime.datetime.now().strftime("%Y%m%d%H%M%S")))  # 存放在
            driver2.save_screenshot(upload_filepath)
        return driver2
        logging.info('PX结束时间{}'.format(str(datetime.datetime.now().strftime("%Y%m%d%H%M%S"))))

    except:
        dics_files = create_test_contexts(path2)
        error_filepath = '{}\\{}.png'.format(dics_files, str(datetime.datetime.now().strftime("%Y%m%d%H%M%S")))  # 存放在
        # error_filepath = 'E:\\PXscreenshot\\error\\{}.png'.format(str(datetime.datetime.now().strftime("%Y%m%d%H%M%S")))  # 存放在
        driver2.save_screenshot(error_filepath)
        global px_c
        px_c += 1
        write_excel(2, 5, px_c)
        logging.error("PX{}进度卡住！！！".format(str(datetime.datetime.now().strftime("%Y%m%d%H%M%S"))))
        driver2.quit()

    # 头侧拍摄

# ------------------调试

def is_ele_exist(self, ele):
    if ele:
        return True
    else:
        return False


dx_a = 0
dx_b = 0
dx_c = 0  # 准备进度卡住
dx_d = 0   #归档成功
dx_e = 0   #归档失败


def test_03_Head_side_shot(i, b, path, path2, path3):
    drive3 = b
    logging.info('3.DX开始时间{}'.format(str(datetime.datetime.now().strftime("%Y%m%d%H%M%S"))))
    logging.info('DX第{}次'.format(i))
    write_excel(3, 2, i)
    time.sleep(3)
    drive3.find_element_by_xpath('//div[text()="登记"]').click()
    time.sleep(1)
    patient_name3 = random_name()
    drive3.find_element_by_xpath('//input[@placeholder="患者姓名" and @class="yg-input__inner" ]').send_keys(
        "{}".format(patient_name3))  # 输入患者姓名参数化
    drive3.find_element_by_xpath('//div[@id="cbctDX"]').click()  # 头测模式
    drive3.find_element_by_xpath('//div[@class="check-in-wrap"]/following-sibling::div[1]//div[2]').click()  # 选择模式下的下一步
    time.sleep(3)
    drive3.find_element_by_id('item2').click()
    # drive3.find_element_by_xpath('//*[@id="item2"]').click() #只能选择第二个

    time.sleep(3)
    drive3.find_element_by_xpath('//button[@id="cbctOkceph"]').click()  # 确定按钮
    try:
        progress_locator = (By.XPATH, '//div[contains(text()," 准备完成")]')  # 等待准备完成元素可见
        WebDriverWait(drive3, 180, 1).until(EC.visibility_of_element_located(progress_locator))
        time.sleep(1)
        drive3.find_element_by_xpath('//div[@id="cbctFinishceph"]').click()  # 头测点下一步
        time.sleep(3)
        # 判断放手闸的提示语元素
        shutil.copy(dir1, dir2)  # 复制手闸文件到CTDebug目录
        # time.sleep(240)  # 头测曝光与采集时间
        ele3 = '//*[@id="leftwidth"]/div/div[5]/div/span/div[4]'
        kw(drive3, 50000, 1, lambda x: x.find_element_by_xpath(ele3))
        # 智能等待,如果找不到以下元素则继续执行
        # time.sleep(2)
        # loc = (By.XPATH, '//div[@class="circle_proc"]')
        # WebDriverWait(drive3, 1200, 3).until_not(EC.visibility_of_element_located(loc))
        # time.sleep(2)

        # 截图
        dic_file = create_contexts(path)  #
        filepath = '{}\\{}.png'.format(dic_file, str(datetime.datetime.now().strftime("%Y%m%d%H%M%S")))
        drive3.save_screenshot(filepath)

        drive3.find_element_by_xpath('//div[@id="cbctbcceph"]').click()  # 点击头测对勾元素确定
        time.sleep(6)
        drive3.find_element_by_xpath('//div[text()="已检查"]').click()  # 已检查元素
        try:
            loc_upload = (By.XPATH, '//div[contains(text(),"已上传")]')
            if WebDriverWait(drive3, 30, 1).until(EC.visibility_of_all_elements_located(loc_upload)):
                logging.info('DX上传成功')
                global dx_a
                dx_a += 1
                write_excel(3, 3, dx_a)
        except:
            logging.info('DX上传失败')
            global dx_b
            dx_b += 1
            write_excel(3, 4, dx_b)


        # 归档牙医管家
        drive3.find_element_by_xpath('//*[@id="app"]/div/div/div[3]/div[2]/div[2]/div[4]/div/div').click()
        button = drive3.find_element_by_xpath(
            '//*[@id="app"]/div/div/div[3]/div[2]/div[1]/div[3]/div/div[3]/button[1]')
        time.sleep(1)
        button.click()
        try:
            uploaded = (By.XPATH, '/html/body/div[2]/div/p')
            if WebDriverWait(drive3, 150, 1).until(EC.visibility_of_all_elements_located(uploaded)):
                logging.info('DX归档牙医管家成功')
                global dx_d
                dx_d += 1
                write_excel(3, 6, dx_d)
        except:
            logging.info('DX归档牙医管家失败')
            global dx_e
            dx_e += 1
            write_excel(3, 7, dx_e)
            upload_error = create_test_contexts(path3)
            upload_filepath = '{}\\{}.png'.format(upload_error,
                                                  str(datetime.datetime.now().strftime("%Y%m%d%H%M%S")))  # 存放在
            drive3.save_screenshot(upload_filepath)
        return drive3
        logging.info('DX结束时间{}'.format(str(datetime.datetime.now().strftime("%Y%m%d%H%M%S"))))

    except:
        dics_files = create_test_contexts(path2)
        error_filepath = '{}\\{}.png'.format(dics_files, str(datetime.datetime.now().strftime("%Y%m%d%H%M%S")))  # 存放在
        drive3.save_screenshot(error_filepath)
        global dx_c
        dx_c += 1
        write_excel(3, 5, dx_c)
        logging.error("DX{}进度卡住！！！".format(str(datetime.datetime.now().strftime("%Y%m%d%H%M%S"))))
        drive3.quit()


ms_a = 0
ms_b = 0
ms_c = 0  # 准备进度卡住
ms_d = 0  #归档成功
ms_e = 0  #归档失败


def test_04_Mode_scanning(i, b, path, path2, path3):
    drive4 = b
    logging.info('4.MS开始时间{}'.format(str(datetime.datetime.now().strftime("%Y%m%d%H%M%S"))))
    logging.info('MS第{}次'.format(i))
    write_excel(4, 2, i)
    time.sleep(3)
    drive4.find_element_by_xpath('//div[text()="登记"]').click()
    time.sleep(1)
    patient_name3 = random_name()
    drive4.find_element_by_xpath('//input[@placeholder="患者姓名" and @class="yg-input__inner" ]').send_keys(
        "{}".format(patient_name3))  # 输入患者姓名参数化
    drive4.find_element_by_xpath('//div[@id="cbctMS"]').click()  #模扫模式
    drive4.find_element_by_xpath('//div[@class="check-in-wrap"]/following-sibling::div[1]//div[2]').click()  #选择模式下的下一步
    time.sleep(3)

    drive4.find_element_by_xpath('//button[@class="ok-btn m-b40 m-t205 userSelect bianseBorder"]').click() #确定按钮
    try:
        progress_locator = (By.XPATH, '//div[contains(text()," 准备完成")]')  # 等待准备完成元素可见
        WebDriverWait(drive4, 150, 1).until(EC.visibility_of_element_located(progress_locator))
        time.sleep(1)
        drive4.find_element_by_xpath('//div[@class="bianseBorderto"]').click()  # 模扫点下一步
        time.sleep(3)
        # 判断放手闸的提示语元素
        shutil.copy(dir1, dir2)  # 复制手闸文件到CTDebug目录
        # time.sleep(180)  # 头测曝光与采集时间
        # 智能等待
        time.sleep(2)
        WebDriverWait(drive4, 300, 3).until(
            EC.visibility_of_element_located((By.XPATH, '//span[@class="nsel close-btn"]')))
        # logging.info("等待模扫预览对话框！{}".format(str(datetime.datetime.now().strftime("%Y%m%d%H%M%S"))))
        WebDriverWait(drive4, 300, 3).until(
            EC.visibility_of_element_located((By.XPATH, '//div[@class="circle_proc"]')))
        WebDriverWait(drive4, 300, 3).until_not(
            EC.visibility_of_element_located((By.XPATH, '//div[@class="circle_proc"]')))
        # logging.info("模扫预览加载结束！{}".format(str(datetime.datetime.now().strftime("%Y%m%d%H%M%S"))))
        time.sleep(1)
        drive4.find_element_by_xpath('//button[@class="yg-button default"]').click()  # 点击确定
        time.sleep(5)
        WebDriverWait(drive4, 320, 3).until_not(
            EC.visibility_of_element_located((By.XPATH, '//div[@class="circle_proc"]')))
        time.sleep(2)
        dic_file = create_contexts(path)  #
        filepath = '{}\\{}.png'.format(dic_file, str(datetime.datetime.now().strftime("%Y%m%d%H%M%S")))
        drive4.save_screenshot(filepath)

        drive4.find_element_by_xpath('//span[@class="nsel close-btn"]').click()  # 点击模型的叉叉按钮



        WebDriverWait(drive4, 120, 3).until(EC.visibility_of_element_located((By.XPATH, '//div[text()="已检查"]')))
        drive4.find_element_by_xpath('//div[text()="已检查"]').click()  # 已检查元素

        try:
            loc_upload = (By.XPATH, '//div[contains(text(),"已上传")]')
            if WebDriverWait(drive4, 30, 1).until(EC.visibility_of_all_elements_located(loc_upload)):
                logging.info('MS上传成功')
                global ms_a
                ms_a += 1
                write_excel(4, 3, ms_a)
        except:
            logging.info('MS上传失败')
            global ms_b
            ms_b += 1
            write_excel(4, 4, ms_b)


        # 归档牙医管家
        drive4.find_element_by_xpath('//*[@id="app"]/div/div/div[3]/div[2]/div[2]/div[4]/div/div').click()
        button = drive4.find_element_by_xpath(
            '//*[@id="app"]/div/div/div[3]/div[2]/div[1]/div[3]/div/div[3]/button[1]')
        time.sleep(1)
        button.click()
        try:
            uploaded = (By.XPATH, '/html/body/div[2]/div/p')
            if WebDriverWait(drive4, 420, 1).until(EC.visibility_of_all_elements_located(uploaded)):
                logging.info('MS归档牙医管家成功')
                global ms_d
                ms_d += 1
                write_excel(4, 6, ms_d)
        except:
            logging.info('MS归档牙医管家失败')
            global ms_e
            ms_e += 1
            write_excel(4, 7, ms_e)
            upload_error = create_test_contexts(path3)
            upload_filepath = '{}\\{}.png'.format(upload_error,
                                                  str(datetime.datetime.now().strftime("%Y%m%d%H%M%S")))  # 存放在
            drive4.save_screenshot(upload_filepath)

        logging.info('MS结束时间{}'.format(str(datetime.datetime.now().strftime("%Y%m%d%H%M%S"))))

    except:
        dics_files = create_test_contexts(path2)
        error_filepath = '{}\\{}.png'.format(dics_files, str(datetime.datetime.now().strftime("%Y%m%d%H%M%S")))  # 存放在
        drive4.save_screenshot(error_filepath)
        global ms_c
        ms_c += 1
        write_excel(4, 5, ms_c)
        logging.error("MS{}进度卡住！！！".format(str(datetime.datetime.now().strftime("%Y%m%d%H%M%S"))))
        drive4.quit()




if __name__ == '__main__':
    f = open('./gggg')
    d = f.read()
    d = int(d)
    create_excel(d)
    start_time = str(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
    write_excel(5, 0, "开始时间")
    write_excel(5, 1, start_time)
    num = 20
    try:
        for x in range(d // num):

            c = login_()
            for i in range(num):
                if c:
                    path1 = 'E:\CTscreenshot' + str(datetime.datetime.now().strftime('%Y%m%d'))
                    path2 = path1 + '\\error'
                    path3 = path2 + '\\uploaderror'
                    a = test_01_CT_shooting(x * num + i + 1, c, path1, path2, path3)
                else:
                    break
                if c:
                    path1 = 'E:\PXscreenshot' + str(datetime.datetime.now().strftime('%Y%m%d'))
                    path2 = path1 + '\\error'
                    path3 = path2 + '\\uploaderror'
                    b = test_02_Panoramic_shooting(x * num + i + 1, c, path1, path2, path3)
                else:
                    break
                if c:
                    path1 = 'E:\DXscreenshot' + str(datetime.datetime.now().strftime('%Y%m%d'))
                    path2 = path1 + '\\error'
                    path3 = path2 + '\\uploaderror'
                    c = test_03_Head_side_shot(x * num + i + 1, c, path1, path2, path3)
                else:
                    break
                if c:
                    path1 = 'E:\MSscreenshot' + str(datetime.datetime.now().strftime('%Y%m%d'))
                    path2 = path1 + '\\error'
                    path3 = path2 + '\\uploaderror'
                    d = test_04_Mode_scanning(x * num + i + 1, c, path1, path2, path3)
                else:
                    break
            if c != None:
                c.quit()
            else:
                pass
    except Exception as e:
        logging.info(e)
    finally:
        finally_time = str(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
        write_excel(6, 0, "停止时间")
        write_excel(6, 1, finally_time)

        # yag = yagmail.SMTP(user="15071312445@163.com", password="XKIXZZCRKYPBWWJS", host='smtp.163.com')
        # # rev_email = 'yi.deng@dentalink.com.cn'
        # # rev_email3 = 'ping.liu@fussenct.com'
        # # rev_email4 = 'lingbing.zeng@fussenct.com'
        #
        # rev_email1 = 'chuanqi.jiang@fussenct.com'
        # rev_email2 = 'liang.ding@fussenct.com'
        # title = '【虚拟驱动】发送附件CBCT测试结果'
        # contents = ['【虚拟驱动】对面电脑CBCT压力稳定性结果统计']
        # file_path = FindNewFile().get_new_file('.\\reports')
        # file_path2 = FindNewFile().get_new_file('.\\logs')
        #
        # # yag.send() #收件人邮箱 标题 正文 附件地址
        # yag.send([rev_email1, rev_email2], title, contents, [file_path, file_path2])
